﻿namespace CoHubServerCore.Configurator;

using System.Text.Json.Serialization;
using Tomlyn;

public partial class Config
{
    [JsonIgnore] public string ConfigDirectory => Directory.GetParent(ConfigPath)!.FullName;

    [JsonIgnore] public string ConfigPath { get; set; } = string.Empty;

    private static Config Init(string path)
    {
        Config config = new();
        return config;
    }

    public static Config Load()
    {
        return Load(DefaultPath.ConfigPath);
    }

    public static Config Load(string path)
    {
        var config = File.Exists(path) ? Toml.ToModel<Config>(File.ReadAllText(path)) : Init(path);
        config.ConfigPath = path;
        return config;
    }

    public void Save()
    {
        File.WriteAllText(ConfigPath, Toml.FromModel(this));
    }
}